package cn.santi.service.impl;

import cn.santi.domain.User;
import cn.santi.mapper.UserMapper;
import cn.santi.service.UserService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.xml.ws.soap.Addressing;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper um;
    @Override
    public List<User> adminGetUser() {
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderBy(true,false,User::getTime);
        return um.selectList(wrapper);
    }

    @Override
    public User getUserByid(int id) {
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(User::getUid,id);
        return um.selectOne(wrapper);
    }
    @Override
    public int UpdateUserState(User user) {
        LambdaQueryWrapper<User> Wrapper = new LambdaQueryWrapper<>();
        Wrapper.eq(User::getUid,user.getUid());
        return um.update(user,Wrapper);
    }
    @Override
    public int adminDeleteUser(int id) {
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(User::getUid,id);
        return um.delete(wrapper);
    }

    @Override
    public List<User> adminGetUserRankAll(String rank) {
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        if (rank.equals("注册时间") || rank.equals("注册时间从晚到早")){
            wrapper.orderBy(true,false,User::getTime);
        }else{
            wrapper.orderBy(true,true,User::getTime);
        }
        return um.selectList(wrapper);
    }

    @Override
    public List<User> adminGetUserRankCondition(String rank, String classify) {
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
         if (rank.equals("注册时间") && classify.equals("正常")){
                wrapper.eq(User::getState,0).orderBy(true,false,User::getTime);
            } else if (rank.equals("注册时间") && classify.equals("禁用")){
             wrapper.eq(User::getState,1).orderBy(true,false,User::getTime);
            }else if (rank.equals("注册时间从早到晚")&& classify.equals("正常")){
             wrapper.eq(User::getState,0).orderBy(true,true,User::getTime);
            }else if (rank.equals("注册时间从早到晚")&& classify.equals("禁用")){
             wrapper.eq(User::getState,1).orderBy(true,true,User::getTime);
            }else if (rank.equals("注册时间从晚到早")&& classify.equals("正常")){
             wrapper.eq(User::getState,0).orderBy(true,false,User::getTime);
            }else if (rank.equals("注册时间从晚到早")&& classify.equals("禁用")){
             wrapper.eq(User::getState,1).orderBy(true,false,User::getTime);
            }
        return um.selectList(wrapper);
    }
}
